<?php
require_once('class.db.php');

function convertUrl($title){
	$t = Array('Ğ', 'Ü', 'Ş', 'İ', 'Ö', 'Ç', 'ğ', 'ü', 'ş', 'ı', 'ö', 'ç');
	$e = Array( 'G', 'U', 'S', 'I', 'O', 'C', 'g', 'u', 's', 'i', 'o', 'c');
	$title = strip_tags($title);
	$title = str_replace($t, $e, $title); // kill entities
	$title = strtolower($title);
	$title = preg_replace('/&.+?;/', '', $title); // kill entities
	$title = preg_replace('/[^%a-z0-9 _-]/', '', $title);
	$title = preg_replace('/\s+/', '-', $title);
	$title = preg_replace('|-+|', '-', $title);
	$title = trim($title, '-');
	return $title;
}

$username = htmlentities($_POST['username']);
$mail = htmlentities($_POST['mail']);
$name = htmlentities($_POST['name']);
$code = uniqid();

$is_user = $db->query("SELECT Count(*) as count FROM tmp_reserve WHERE username = '".$username."' OR mail = '".$mail."'");

if( $is_user->fetchColumn() > 0 )
{
	echo "<div id='error'><p>Sorry this username or email previously registered.</p></div>" ;
}
else
{
	$m = $db->query("INSERT INTO tmp_reserve VALUES ('' , '".convertUrl($username)."','".$name."','".$mail."', '".$code."', NULL)");
	if($m)
	{
		echo "<div id='ok'><p>  Thank you for requesting an reservation.  </p><p>You have been added to the user list. Feykin will send you an invitation as soon as one becomes available. </p></div>";
	}
	else
	{
		echo "<div id='error'><p>Sorry! Plase try again! </p></div>";
	}
}